package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.client.ClientProtocolException;
import org.junit.runner.Result;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.cdfgeteway.parameter.CreateCdfMemberReq;
import com.shop2cn.iapi.cdfgeteway.parameter.ReceiveCouponReq;
import com.shop2cn.iapi.cdfgeteway.parameter.UserCouponListReq;
import com.shop2cn.iapi.cdfgeteway.service.CreateCdfMemberCall;
import com.shop2cn.iapi.cdfgeteway.service.ReceiveCouponCall;
import com.shop2cn.iapi.cdfgeteway.service.UserCouponListCall;
import com.shop2cn.iapi.coupon.parameter.receiveCouponBean;
import com.shop2cn.iapi.coupon.service.receiveCouponCall;
import com.shop2cn.iapi.member.parameter.CdfUserAuthorBean;
import com.shop2cn.iapi.member.service.CdfUserAuthorCall;
import com.shop2cn.iapi.userbiz.parameter.AddAddressBean;
import com.shop2cn.iapi.userbiz.service.AddAddressCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.UserCreateCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.TestDataManager;

public class ZhongMianStgCallService {
public static Integer SITCDFSHOP=500041475;
public static UserCreateBean initUsercreateBean() {
		UserCreateBean usercreateBean = new UserCreateBean();
		usercreateBean.setAppSource(60);
		usercreateBean.setChannelSource(6);
		usercreateBean.setClientIp(TestDataManager.getRandomIp());
		usercreateBean.setCountryCode("");
		usercreateBean.setPhysicsDeviceId(TestDataManager.getRandomUUID().toString());
		usercreateBean.setLogicDeviceId(TestDataManager.getRandomUUID().toString());
		usercreateBean.setLoginId("LID" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
		usercreateBean.setLoginPwd("1234qwer");
		usercreateBean.setLogoUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/xjx7HJDnTwdeIhl5cf8ZC4NibQZQdWcJnTtVYyiaR1r7qrqf8ZDssUU9b84BqloujHic0v58h15AiapicxMp7Qmobxg/13");
		usercreateBean.setNickname("NickautoTest");
		usercreateBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
		usercreateBean.setUnionId("uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
		usercreateBean.setRemark("remarkAutoTest");
		usercreateBean.setTerminalSource(2);
		usercreateBean.setUserGender(2);
		usercreateBean.setUserType(0);
		usercreateBean.setWxAppName("503710454");
		usercreateBean.setBelongSellerId(503710454);
		return usercreateBean;
	}

	/**
	 * 创建用户
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static Integer createUser() throws ClientProtocolException, IOException {
	UserCreateBean usercreateBean = initUsercreateBean();
	UserCreateCall usercreateCall = new UserCreateCall();
	usercreateCall.setData(usercreateBean);
	usercreateCall.callService();
	Integer uid=Integer.valueOf(usercreateCall.getString("userId"));
	return uid;
}
	/**
	 * 创建token
	 * @param uid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String createToken(Integer uid) throws ClientProtocolException, IOException {
		return AuthCallServiceV2.createToken(uid,"goduserwxapp");
	}
	/**
	 * 添加地址
	 * @param uid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String createAddress(Integer uid) throws ClientProtocolException, IOException {
		AddAddressBean address=new AddAddressBean();
		AddAddressCall AddAddressCall=new AddAddressCall();
//		"accessToken": "11",
//	    "addressId": 0,
		address.setAddressId(0);
//	    "addressee": "用户",
		address.setAddressee("周总"); 
//	    "appId": "usercenter.yxwx.ymatou.com",
		address.setAppId("usercenter.yxwx.ymatou.com");
//	    "area": "北京市,北京市,东城区",
		address.setArea("北京市,北京市,东城区");
//	    "default": false,
		address.setDefault1(false);
//	    "detailAddress": "详细地址",
		address.setDetailAddress("详细地址");
//	    "idCardNo": "",
//	    "phone": "13575285130",
		address.setPhone(TestDataManager.getPhoneNum());
//	    "postCode": "",
//	    "requestId": "ed2189cf-2e80-8570-ab92-59def013a044",
//	    "userId": 用户编号
		address.setUserId(uid);
		AddAddressCall.setData(address);
		AddAddressCall.callService();
		return AddAddressCall.getJsonObject().get("data").getAsJsonObject().get("addressId").getAsString();
	}
	/**
	 * 领券
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void receiveCoupon(Integer userId,String batchCodes) throws ClientProtocolException, IOException {
		receiveCouponBean bean=new receiveCouponBean();
		receiveCouponCall call=new receiveCouponCall();
		bean.setIdentityKey(TestDataManager.getRandomUUID().toString());
		bean.setUserId(userId);
		bean.setUserType(0);
		bean.setBatchCodes(Arrays.asList(batchCodes));
		call.setData(bean);
		call.callService();	
	}
	/**cdf电子券
	 * @param userId 26080783 shopid 502060981l-sit
	 * @param batchCodes
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void receiveCdfCoupon(Integer userId,List<String> batchCodes) throws ClientProtocolException, IOException {
		ReceiveCouponReq ReceiveCouponReq = new com.shop2cn.iapi.cdfgeteway.parameter.ReceiveCouponReq();
		ReceiveCouponCall ReceiveCouponCall = new ReceiveCouponCall();
//		ReceiveCouponReq.setShopId(500041475l);
		ReceiveCouponReq.setShopId(502060981l);
		
		ReceiveCouponReq.setUserId(Long.valueOf(userId));
//		ReceiveCouponReq.setBatchIdList(Arrays.asList("10BLD001030","20BLD001035","40BLD001032"));
		ReceiveCouponReq.setBatchIdList(batchCodes);
		ReceiveCouponCall.setData(ReceiveCouponReq);
		ReceiveCouponCall.callService();
	}
	
	public static void receiveCdfMacaoCoupon(Integer userId,List<String> batchCodes) throws ClientProtocolException, IOException {
		ReceiveCouponReq ReceiveCouponReq = new com.shop2cn.iapi.cdfgeteway.parameter.ReceiveCouponReq();
		ReceiveCouponCall ReceiveCouponCall = new ReceiveCouponCall();
		ReceiveCouponReq.setShopId(502665591L);
		ReceiveCouponReq.setUserId(Long.valueOf(userId));
//		ReceiveCouponReq.setBatchIdList(Arrays.asList("10BLD001030","20BLD001035","40BLD001032"));
		ReceiveCouponReq.setBatchIdList(batchCodes);
		ReceiveCouponCall.setData(ReceiveCouponReq);
		ReceiveCouponCall.callService();
	}
	
	/**
	 * 创建中免会员
	 * @param userId
	 * @param batchCodes
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void CreateCdfMember(Integer userId,String mobile) throws ClientProtocolException, IOException {
		CreateCdfMemberReq bean=new CreateCdfMemberReq();
		CreateCdfMemberCall call=new CreateCdfMemberCall();
		bean.setIdNumber(TestDataManager.getCardId(20, 10, 1));
		bean.setMobile(mobile);
		bean.setUserId(Long.valueOf(userId));
		call.setData(bean);
		call.callService();	
	}
	/**
	 * 查询可用中免电子券
	 * @param userId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static List<String> getCdfCouponCode(Integer userId,Integer shopid) throws ClientProtocolException, IOException {
		UserCouponListReq UserCouponListReq = new com.shop2cn.iapi.cdfgeteway.parameter.UserCouponListReq();
		UserCouponListCall UserCouponListCall = new UserCouponListCall();
		UserCouponListReq.setCouponState("UNUSED");
		UserCouponListReq.setPageIndex(1);
		UserCouponListReq.setPageSize(1000);
		UserCouponListReq.setShopId(shopid);
		UserCouponListReq.setUserId(userId);
		UserCouponListCall.setData(UserCouponListReq);
		UserCouponListCall.callService();
		List<String> coupons=new ArrayList<String>();
		JsonArray ja = UserCouponListCall.getJsonObject().get("coupons").getAsJsonArray();
		ja.forEach(info->{
			String couponCode = info.getAsJsonObject().get("couponCode").getAsString();
			coupons.add(couponCode);
		});
		return coupons;
	}
	/**
	 * 授权cdf会员
	 * @param userId
	 * @param shopid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void CdfUserAuthor(Integer userId,Integer shopid) throws ClientProtocolException, IOException {
		CdfUserAuthorBean CdfUserAuthorBean=new CdfUserAuthorBean();
		CdfUserAuthorCall CdfUserAuthorCall=new CdfUserAuthorCall();
		CdfUserAuthorBean.setShopId(shopid);
		CdfUserAuthorBean.setUserId(userId);
		CdfUserAuthorCall.setData(CdfUserAuthorBean);
		CdfUserAuthorCall.callService();
	}
	public static void main(String args[]) throws ClientProtocolException, IOException {
//		500011465&
		ZhongMianStgCallService.getCdfCouponCode(500011465, 500041475);
	}
}
